package com.iotimc.attendancesystem.io;

import android.media.AudioManager;
import android.media.SoundPool;
import android.util.Log;

import com.iotimc.attendancesystem.App;
import com.iotimc.attendancesystem.R;
import com.iotimc.attendancesystem.event.CardEvent;
import com.iotimc.attendancesystem.util.HexByteUtil;
import com.iotimc.comass.bean.CardCommon;

import org.greenrobot.eventbus.EventBus;

/**
 * @author david
 * @date 2017/12/1
 */

public class CardCommonPresenter {
    private static final String TAG = "IoCommonPresenter";
    CardCommon cardCommon;
    private SoundPool soundPool;
    private static class SingletonHolder {
        private static final CardCommonPresenter INSTANCE = new CardCommonPresenter();
    }

    public static CardCommonPresenter getInstance() {
        return CardCommonPresenter.SingletonHolder.INSTANCE;
    }

    public SoundPool getSoundPool() {
        return soundPool;
    }

    private CardCommonPresenter() {
        soundPool= new SoundPool(10, AudioManager.STREAM_MUSIC,5);
        soundPool.load(App.getAppContext(), R.raw.card,1);
        soundPool.load(App.getAppContext(), R.raw.deng,1);
        cardCommon = new CardCommon(new CardCommon.OnListener() {
            /**
             * @param result 操作结果，0为成功，1为失败
             * @param addr 地址，十六进制  0xFFFE
             * @param data 刷卡数据。普通 IC卡4位十六进制数据卡号：01 84 2A 3E 95 04
                身份证8位十六进制数据卡号：03 64 8B 26 18 79 A4 E5 21 CB
                首位“01/03”为卡类型，末尾“04/CB”为异或校验码。卡号为中间字节。
             */
            @Override
            public void getCardNum(short result, short addr, byte[] data) {
                soundPool.play(1,1, 1, 0, 10, 1);
                Log.i(TAG, "getCardNum: card " + result);
                String card = HexByteUtil.byteArrToHex(data);
                Log.i(TAG, "getCardNum: card " + card);
                EventBus.getDefault().post(new CardEvent(card));
            }
        });

    }
}
